package com.example.myapplication.ui.fragment.collect

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.loadmore.SimpleLoadMoreView
import com.example.myapplication.R
import com.example.myapplication.arouter.ARouterPath
import com.example.myapplication.bean.collect.WebsiteCollectBean
import com.example.myapplication.dialog.OperationDialog
import com.example.myapplication.ui.activity.collect.MyCollectionViewModel
import com.example.myapplication.ui.adapter.WebsiteCollectAdapter
import com.example.myapplication.ui.vm.SharedViewModel
import com.kun.base.env.Env
import com.kun.base.ui.AbsBaseFragment
import com.scwang.smart.refresh.header.ClassicsHeader
import kotlinx.android.synthetic.main.fragment_home.*

/**
 *网站收藏
 *
 * @author : Kun
 * @date : 2021/6/8
 * @time : 9:47
 */
class WebsiteCollectFragment : AbsBaseFragment() {

    private lateinit var websiteCollectAdapter: WebsiteCollectAdapter

    private lateinit var websiteCollectViewModel: WebsiteCollectViewModel
    private lateinit var myCollectionViewModel: MyCollectionViewModel
    private lateinit var sharedViewModel: SharedViewModel
    override fun getLayoutId(): Int {
        return R.layout.fragment_article_collect
    }

    override fun initViewModel() {
        websiteCollectViewModel = getFragmentScopeViewModel(WebsiteCollectViewModel::class.java)
        myCollectionViewModel = getActivityScopeViewModel(MyCollectionViewModel::class.java)
        sharedViewModel=getApplicationScopeViewModel(SharedViewModel::class.java)
        sharedViewModel.unCollectArticle.observe(this, Observer {
            refresh_layout!!.autoRefresh()
        })

        websiteCollectViewModel.collectRequest.collectWebsiteResult.observe(this, Observer { t ->
            if (t.errorCode == 0) {
                websiteCollectAdapter.setList(t.data)
                websiteCollectAdapter.loadMoreModule.loadMoreEnd()
            } else {
                websiteCollectAdapter.loadMoreModule.loadMoreEnd()
            }
            refresh_layout!!.finishRefresh()
        })
        websiteCollectViewModel.collectRequest.deleteWebsiteResult.observe(this, Observer { t ->
            if (t.errorCode == 0) {
                websiteCollectAdapter.let {
                    val list = it.data
                    for (i in list) {
                        if (t.data == i.id) {
                            list.remove(i)
                            break
                        }
                    }
                    it.notifyDataSetChanged()
                }
            }
        })
        myCollectionViewModel.unCollectArticle.observe(this, Observer { t ->
            if (t == 1) {
                //刷新数据
                getData()
            }
        })
    }

    override fun initView() {
        refresh_layout!!.setRefreshHeader(ClassicsHeader(activity))

        websiteCollectAdapter = WebsiteCollectAdapter(R.layout.item_website_collect, null)
        websiteCollectAdapter.loadMoreModule.loadMoreView = SimpleLoadMoreView()

        recycler_view!!.layoutManager = LinearLayoutManager(activity)
        recycler_view!!.adapter = websiteCollectAdapter

        websiteCollectAdapter.loadMoreModule.setOnLoadMoreListener {
            recycler_view!!.postDelayed({ websiteCollectAdapter.loadMoreModule.loadMoreEnd() }, 200)
        }
        refresh_layout!!.setOnRefreshListener {
            //刷新数据
            getData()
        }

        websiteCollectAdapter.setOnItemClickListener { _, _, position ->
            //网站点击
            val bean = websiteCollectAdapter.getItem(position)
            startToWeb(bean.link, bean.name)
        }
        websiteCollectAdapter.setOnItemLongClickListener { _, _, position ->
            val articleBean = websiteCollectAdapter.getItem(position)
            showOperationDialog(articleBean)
            return@setOnItemLongClickListener true
        }

        getData()
    }

    /**
     * 加载网站列表
     */
    private fun getData() {
        websiteCollectViewModel.requestCollectWebsiteList()
    }

    /**
     * 跳转到web页面
     */
    private fun startToWeb(linkUrl: String, title: String) {
        var url = linkUrl
        if (!url.startsWith("http://") && !url.startsWith("https://")) {
            url = Env.getDomain() + url
        }

        ARouter.getInstance().build(ARouterPath.WEB).withString("url", url)
            .withString("title", title).navigation()
    }

    /**
     * 显示操作弹窗
     */
    private fun showOperationDialog(bean: WebsiteCollectBean) {
        OperationDialog(activity!!).apply {
            addSelectItems("打开链接", "修改", "删除")
            setOnSelectListener(object : OperationDialog.OnSelectListener {
                override fun onSelect(name: String, position: Int) {
                    when (position) {
                        0 -> {
                            startToWeb(bean.link, bean.name)
                        }
                        1 -> {
                            ARouter.getInstance().build(ARouterPath.ADD_COLLECTION)
                                .withInt("type", 1).withObject("websiteCollectBean", bean)
                                .navigation(activity, 101)
                        }
                        else -> {
                            websiteCollectViewModel.requestCollectWebsiteList(bean.id)
                        }
                    }
                    dismiss()
                }
            })
        }.show()
    }
}